﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using JWFW.SFL;
using System.Net;
using FluorineFx.Messaging.Messages;
using System.IO;
using FluorineFx.IO;

namespace TestTech
{
    public class AMFOP
    {
        public void SocketSend()
        {
        }


        /// <summary>
        /// 将数字由第一个进制转换成第二个进制
        /// </summary>
        /// <param name="inputNumber"></param>
        /// <param name="fromBase"></param>
        /// <param name="toBase"></param>
        /// <returns></returns>
        public string ConvertIntBase(string inputNumber, int fromBase, int toBase)
        {
            int intValue = Convert.ToInt32(inputNumber, fromBase);
            return Convert.ToString(intValue, toBase);
        }
        /// <summary>
        /// 截断二进制数组
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public byte[] TruncateBytes(byte[] input, int length)
        {
            byte[] res = new byte[length];
            for (int i = 0; i < length; i++)
            {
                res[i] = input[i];
            }
            return res;
        }
        /// <summary>
        /// 连接两个Byte数组
        /// </summary>
        /// <param name="a">第一个Byte数组</param>
        /// <param name="b">第二个Byte数组</param>
        /// <returns></returns>
        public byte[] CombineBytes(byte[] a, byte[] b)
        {
            byte[] res = new byte[a.Length + b.Length];
            a.CopyTo(res, 0);
            b.CopyTo(res, a.Length);
            return res;
        }
        /// <summary>
        /// 剔除16进制数据中的空格回车等，用于转换成字符串
        /// </summary>
        /// <param name="hexData"></param>
        /// <returns></returns>
        public string RefineHex(string hexData)
        {
            return hexData.Replace(" ", "").Replace("\r", "").Replace("\n", "");
        }
        /// <summary>
        /// 将16进制的数据字符串转换成byte数组
        /// </summary>
        /// <param name="hexData"></param>
        /// <returns></returns>
        public byte[] HexToBytes(string hexData)
        {
            hexData = RefineHex(hexData);
            int length = hexData.Length / 2;
            byte[] res = new byte[length];
            for (int i = 0; i < length; i++)
            {
                string tmp = hexData.Substring(i * 2, 2);
                res[i] = Convert.ToByte(tmp, 16);
            }
            return res;
        }
        /// <summary>
        /// 将byte数组转换成16进制的字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public string BytesToHex(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder(100);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(" ");
                }
                sb.Append(Convert.ToString((int)bytes[i], 16).PadLeft(2, '0'));
            }
            return sb.ToString().Trim();
        }
        /// <summary>
        /// 16进制还原成字符串
        /// </summary>
        /// <param name="hexData"></param>
        /// <param name="zeroNum"></param>
        /// <returns></returns>
        public string HexToASCIIString(string hexData, int zeroNum = 0)
        {
            StringBuilder sb = new StringBuilder(100);
            hexData = RefineHex(hexData);
            for (int i = 0; i < hexData.Length; i++)
            {
                if (i % 2 == 0)
                {
                    string tmp = hexData.Substring(i, 2);
                    if (tmp == "00")
                    {
                        sb.Append((char)zeroNum);
                    }
                    else
                    {
                        sb.Append((char)Convert.ToInt32(tmp, 16));
                    }
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 字符串转换成16进制的字符串，每个字节间包含空格
        /// </summary>
        /// <param name="input"></param>
        /// <param name="zeroNum"></param>
        /// <returns></returns>
        public string ASCIIStringToHex(string input, int zeroNum = 0)
        {
            StringBuilder sb = new StringBuilder(100);
            for (int i = 0; i < input.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(" ");
                }
                if ((int)input[i] == zeroNum)
                {
                    sb.Append("00");
                }
                else
                {
                    sb.Append(Convert.ToString((int)input[i], 16).PadLeft(2, '0'));
                }
            }
            return sb.ToString().Trim();
        }

        public byte[] SerializeAMF3Message(MessageBase message, string bodyResponse)
        {
            AMFMessage amfM = new AMFMessage(3);
            AMFBody body = new AMFBody();
            amfM.AddBody(body);
            body.Response = bodyResponse;
            body.Content = new object[] { message };
            byte[] data = new byte[1024];
            MemoryStream ms = new MemoryStream(data);
            AMFSerializer amfS = new AMFSerializer(ms);
            amfS.WriteMessage(amfM);
            return TruncateBytes(data, (int)ms.Position);
        }
        public FluorineFx.IO.AMFMessage DeserializeAMFMessage(byte[] message)
        {
            MemoryStream ms = new MemoryStream(message);
            AMFDeserializer amfD = new AMFDeserializer(ms);
            return amfD.ReadAMFMessage();
        }

        public void TestLogin()
        {
            HttpOP hop = new HttpOP();
            hop.Proxy = "proxy.atlanta.hp.com";
            hop.Port = "8080";
            RemotingMessage loginRM = new RemotingMessage();
            loginRM.source = "Login";
            loginRM.operation = "doLogin";
            loginRM.timeToLive = 0;
            loginRM.timestamp = 0;
            loginRM.destination = "amfphp";
            loginRM.messageId = "2D894784-CEA4-2ECA-DA6D-259B97771F0C";
            loginRM.clientId = "189649ED-C6F3-2A49-BBB1-0000368D5F3A";
            loginRM.headers.Clear();
            loginRM.headers.Add(MessageBase.EndpointHeader, null);
            loginRM.headers.Add(MessageBase.DestinationClientIdHeader, "nil");
            loginRM.body = new object[5] { 1150, 0, "uuyx", "baoljyj", "chaohu" };
            byte[] postData = this.SerializeAMF3Message(loginRM, @"/3");
            File.WriteAllBytes(@"c:\test\test.dat", postData);
            Console.WriteLine(BytesToHex(postData));
            Console.WriteLine(HexToASCIIString(BytesToHex(postData), 32));
            return;
            byte[] res = hop.NavigateBytes(url, postData);

            var sndMsg = this.DeserializeAMFMessage(postData);
            var rcvMsg = this.DeserializeAMFMessage(res);

        }

        public void ObjectToAMF()
        {
            byte[] b = HexToBytes(contentString);
            File.WriteAllBytes(@"c:\test\test_1.dat", b);
        }

        string url = "http://sg236.ledu.com/server/amfphp/gateway.php";
        string contentString = @"  00 03 00 00 00 01 00 04 6e 75 6c 6c 00 02 2f 33  
  00 00 01 1a 0a 00 00 00 01 11 0a 81 13 4f 66 6c  
  65 78 2e 6d 65 73 73 61 67 69 6e 67 2e 6d 65 73  
  73 61 67 65 73 2e 52 65 6d 6f 74 69 6e 67 4d 65  
  73 73 61 67 65 13 6f 70 65 72 61 74 69 6f 6e 0d  
  73 6f 75 72 63 65 09 62 6f 64 79 11 63 6c 69 65  
  6e 74 49 64 15 74 69 6d 65 54 6f 4c 69 76 65 13  
  6d 65 73 73 61 67 65 49 64 17 64 65 73 74 69 6e  
  61 74 69 6f 6e 13 74 69 6d 65 73 74 61 6d 70 0f  
  68 65 61 64 65 72 73 06 0f 64 6f 4c 6f 67 69 6e  
  06 0b 4c 6f 67 69 6e 09 03 01 09 0b 01 04 88 61  
  04 00 06 09 75 75 79 78 06 01 06 01 06 49 37 38  
  46 39 31 43 35 31 2d 38 45 42 46 2d 31 39 41 38  
  2d 45 42 45 44 2d 30 30 30 30 34 44 46 42 32 46  
  30 32 04 00 06 49 34 46 35 38 46 33 39 37 2d 45  
  37 35 34 2d 43 30 33 44 2d 39 41 37 39 2d 32 44  
  34 45 46 43 42 38 36 41 45 32 06 0d 61 6d 66 70  
  68 70 04 00 0a 0b 01 09 44 53 49 64 06 07 6e 69  
  6c 15 44 53 45 6e 64 70 6f 69 6e 74 01 01 ";
    }
}
